import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from './store'
import { isRelogin } from '@/utils/request'
import { getToken } from '@/utils/auth'
const isUM = window.loginType === 'UM'

NProgress.configure({ showSpinner: false })

NProgress.configure({ showSpinner: false })

router.beforeEach((to, from, next) => {
  /*  if (from.name === 'incidentHandle' || from.path == '/dispose/incidentList' || from.path == '/dispose/incidentHandle/detail') {
    //  事件处置
    if (to.path == '/dispose/incidentList' || to.path == '/dispose/incidentHandle/detail' || to.name === 'incidentHandle' || from.path == '/dispose/incidentList' || from.path == '/dispose/incidentHandle/detail') {
      store.dispatch('settings/addKeepAlivePage', 'IncidentHandle')
    }
  } else if (from.name === 'mlk_access_rule' || from.name == 'mlk_rule_add') {
    //  接入规则
    // if (to.path.includes('/mlk/ruleAdd') || to.path.includes('/mlk/access/rule')) {
    //   store.dispatch('settings/addKeepAlivePage', 'AccessRule')
    // }
  } else if (from.name === 'mlk_task' || from.path.includes('/mlk/taskAdd')) {
    // 任务管理
    if (to.path.includes('/mlk/taskAdd') || to.name === 'mlk_task') {
      store.dispatch('settings/addKeepAlivePage', 'MlkTask')
    }
  } else if (from.name === 'mlk_commonDict' || from.path.includes('/mlk/dictDataList')) {
    // 公共字典管理
    if (to.path.includes('/mlk/dictDataList') || to.name === 'mlk_commonDict') {
      store.dispatch('settings/addKeepAlivePage', 'MlkCommonDict')
    }
  } else if (from.name === 'icp_metrics' || from.path.includes('/icp/metricsDetail')) {
    // 监测指标
    if (to.path.includes('/icp/metricsDetail') || to.name === 'icp_metrics') {
      store.dispatch('settings/addKeepAlivePage', 'IcpMetrics')
    }
  } else if (from.name === 'icp_rule' || from.path.includes('/icp/ruleDetail')) {
    // 监测规则
    if (to.path.includes('/icp/ruleDetail') || to.name == 'icp_rule') {
      store.dispatch('settings/addKeepAlivePage', 'IcpRule')
    }
  } else if (from.name === 'dispose_list' || from.path.includes('/dispose/listInfo')) {
    // 事件研判处置-全部工单
    if (to.path.includes('/dispose/listInfo') || to.name == 'dispose_list') {
      store.dispatch('settings/addKeepAlivePage', 'DisposeList')
    }
  } else if (from.name === 'portray_search' || from.path.includes('/dispose/entityPortrait')) {
    // 事件研判处置-刻画搜索
    if (to.path.includes('/dispose/entityPortrait') || to.name === 'portray_search') {
      store.dispatch('settings/addKeepAlivePage', 'PortraySearch')
    }
  } else {
    store.dispatch('settings/clearKeepAlive')
  } */
  NProgress.start()
  if (!getToken() && !isUM) {
    next()
    NProgress.done()
    return
  }

  /* has token*/
  if (store.getters.roles.length === 0) {
    // 判断当前用户是否已拉取完user_info信息
    store
      .dispatch('GetInfo')
      .then(() => {
        next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
      })
      .catch((err) => {
        isRelogin.show = false
        console.log(err)
        if (err.redirect) {
          location.href = err.redirect
        }
        redirect(next)
      })
  } else {
    redirect(next)
  }
})

router.afterEach(() => {
  NProgress.done()
})

/**
 * 移除URL上code和state的参数,并重定向到新地址
 */
function redirect(next) {
  let url = location.href
  if ((url.indexOf('code=') === -1) & (url.indexOf('state=') === -1)) {
    next()
    return
  }

  const reg = new RegExp('&?code=[^&]*|state=[^&]*|sid=[^&]*', 'gi')
  url = url.replace(reg, '')
  if (url.indexOf('?&') !== -1) {
    url = url.replace('?&', '?')
  }
  if (url.endsWith('&')) {
    url = url.substring(0, url.length - 1)
  }
  if (url.endsWith('?')) {
    url = url.substring(0, url.length - 1)
  }

  location.href = url

  next()
}
